package cn.iocoder.yudao.module.dify.service.riskreportrecords;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.dify.controller.admin.riskreportrecords.vo.RiskReportRecordsPageReqVO;
import cn.iocoder.yudao.module.dify.controller.admin.riskreportrecords.vo.RiskReportRecordsSaveReqVO;
import cn.iocoder.yudao.module.dify.dal.dataobject.riskreportrecords.RiskReportRecordsDO;
import cn.iocoder.yudao.module.dify.dal.mysql.riskreportrecords.RiskReportRecordsMapper;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.RISK_REPORT_RECORDS_NOT_EXISTS;

/**
 * dify风险管理报告任务记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RiskReportRecordsServiceImpl implements RiskReportRecordsService {

    @Resource
    private RiskReportRecordsMapper riskReportRecordsMapper;

    @Resource
    private FileApi fileApi;

    @Override
    public Long createRiskReportRecords(RiskReportRecordsSaveReqVO createReqVO) {
        // 插入
        RiskReportRecordsDO riskReportRecords = BeanUtils.toBean(createReqVO, RiskReportRecordsDO.class);
        riskReportRecordsMapper.insert(riskReportRecords);
        // 返回
        return riskReportRecords.getId();
    }

    @Override
    public void updateRiskReportRecords(RiskReportRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskReportRecordsExists(updateReqVO.getId());
        // 更新
        RiskReportRecordsDO updateObj = BeanUtils.toBean(updateReqVO, RiskReportRecordsDO.class);
        riskReportRecordsMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskReportRecords(Long id) {
        // 校验存在
        validateRiskReportRecordsExists(id);
        // 删除
        riskReportRecordsMapper.deleteById(id);
    }

    private void validateRiskReportRecordsExists(Long id) {
        if (riskReportRecordsMapper.selectById(id) == null) {
            throw exception(RISK_REPORT_RECORDS_NOT_EXISTS);
        }
    }

    @Override
    public RiskReportRecordsDO getRiskReportRecords(Long id) {
        return riskReportRecordsMapper.selectById(id);
    }

    @Override
    public PageResult<RiskReportRecordsDO> getRiskReportRecordsPage(RiskReportRecordsPageReqVO pageReqVO) {
        return riskReportRecordsMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateRiskReportStatus(Long recordId, Integer status) {
        RiskReportRecordsDO record = getRiskReportRecords(recordId);
        record.setProcessStatus(status);
        record.setUpdateTime(LocalDateTime.now());
        riskReportRecordsMapper.updateById(record);
    }

    @Override
    public Long getRiskReportRecordsCount(Long userId) {
        return riskReportRecordsMapper.selectCount(new LambdaQueryWrapperX<RiskReportRecordsDO>()
                .eq(RiskReportRecordsDO::getProcessStatus, 0)
                .eq(RiskReportRecordsDO::getCreator, userId));

    }

    @Override
    public Boolean riskManagementReportUploadReviewFile(Long id, MultipartFile file) throws IOException {
        String reviewFileUrl = fileApi.createFile(file.getOriginalFilename(), file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream()));
        RiskReportRecordsDO riskReportRecords = getRiskReportRecords(id);
        riskReportRecords.setReviewUrl(reviewFileUrl);
        riskReportRecords.setReviewStatus(2);
        riskReportRecords.setProcessStatus(1);
        return riskReportRecordsMapper.updateById(riskReportRecords) > 0;
    }
}